--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--[[

【郁愈域】


]]--
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--- hud 前置准备
    local Widget = require "widgets/widget"
    local Image = require "widgets/image"
    local UIAnim = require "widgets/uianim"
    local Screen = require "widgets/screen"
    local AnimButton = require "widgets/animbutton"
    local ImageButton = require "widgets/imagebutton"
    local Menu = require "widgets/menu"
    local Text = require "widgets/text"
    local TEMPLATES = require "widgets/redux/templates"
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--- hud install
    --- 坐标读取
    local function GetHUDLoation()
        local data = TUNING.HUTAO_FNS:Get_ThePlayer_Cross_Archived_Data("healing_grove_hud_location")
        if data == nil then
            return 0.5,0.5
        else
            return data.x,data.y
        end
    end
    local function SetHUDLoation(x,y)
        TUNING.HUTAO_FNS:Set_ThePlayer_Cross_Archived_Data("healing_grove_hud_location",{x = x,y = y})
    end
    local function CreateHUD(inst)
        ----------------------------------------------------------------------------------------------------------------
        --- 前置根节点
            local front_root = inst.HUD and inst.HUD.controls.status
            if front_root == nil then
                print("Error : hutao_com_healing_grove hud setup failed")
                return
            end
        ----------------------------------------------------------------------------------------------------------------
        --- 创建根节点
            local root = front_root:AddChild(Widget())
        ----------------------------------------------------------------------------------------------------------------
        --- 重置基点
            root:SetHAnchor(1) -- 设置原点x坐标位置，0、1、2分别对应屏幕中、左、右
            root:SetVAnchor(2) -- 设置原点y坐标位置，0、1、2分别对应屏幕中、上、下
            root:SetPosition(1000,500)
            -- root:FollowMouse()
            --- 刷一次缩放。保证和当前HUD缩放一致
            local old_scale = root:GetScale()
            local ret_scale =  old_scale.x
            root:SetScale(ret_scale, ret_scale, ret_scale)
        ----------------------------------------------------------------------------------------------------------------
        --- 启动坐标跟随缩放循环任务，缩放的时候去到指定位置。官方好像没预留这类API，或者暂时找不到方法
            function root:LocationScaleFix()
                if self.x_percent and not self.__mouse_holding  then
                    local scrnw, scrnh = TheSim:GetScreenSize()
                    if self.____last_scrnh ~= scrnh then
                        local tarX = self.x_percent * scrnw
                        local tarY = self.y_percent * scrnh
                        self:SetPosition(tarX,tarY)
                    end
                    self.____last_scrnh = scrnh
                end
            end            
            root.x_percent,root.y_percent = GetHUDLoation()
            root:LocationScaleFix()
            root.inst:DoPeriodicTask(2,function()
                root:LocationScaleFix()
            end)
        ----------------------------------------------------------------------------------------------------------------
        ---- 鼠标拖动
            local old_OnMouseButton = root.OnMouseButton
            root.OnMouseButton = function(self,button, down, x, y)
                if down and button == MOUSEBUTTON_RIGHT then
                    if not root.__mouse_holding  then
                        root.__mouse_holding = true      --- 上锁
                            --------- 添加鼠标移动监听任务
                            root.___follow_mouse_event = TheInput:AddMoveHandler(function(x, y)  
                                root:SetPosition(x,y,0)
                            end)
                            --------- 添加鼠标按钮监听
                            root.___mouse_button_up_event = TheInput:AddMouseButtonHandler(function(button, down, x, y) 
                                if button == MOUSEBUTTON_RIGHT and down == false then    ---- 左键被抬起来了
                                    root.___mouse_button_up_event:Remove()       ---- 清掉监听
                                    root.___mouse_button_up_event = nil

                                    root.___follow_mouse_event:Remove()          ---- 清掉监听
                                    root.___follow_mouse_event = nil

                                    root:SetPosition(x,y,0)         ---- 设置坐标
                                    root.__mouse_holding = false    ---- 解锁

                                    local scrnw, scrnh = TheSim:GetScreenSize()
                                    root.x_percent = x/scrnw
                                    root.y_percent = y/scrnh
                                    SetHUDLoation(root.x_percent,root.y_percent)

                                end
                            end)
                    end

                end
                return old_OnMouseButton(self,button, down, x, y)
            end
        ----------------------------------------------------------------------------------------------------------------
        --- 背景
            local bg = root:AddChild(Image("images/global_redux.xml", "progressbar_wxplarge_frame.tex"))
            bg:SetScale(0.35,1)
        ----------------------------------------------------------------------------------------------------------------
        --- 文本
            local function GetDisplayStr(value)
                local current_value = value or inst.replica.hutao_com_healing_grove:GetCurrent()
                --- 用format 强制保留一位小数
                local spell_name = TUNING.HUTAO_FNS:GetString("hutao_equipment_black_hoodie_of_a_depressed_girl","shell") or "SPELL"
                return spell_name .." ： " .. string.format("%.1f",current_value)
            end

            local display_text = root:AddChild(Text(CODEFONT,35,"郁愈域 ： 150.0",{ 255/255 , 255/255 ,255/255 , 1}))
            local function txt_update_fn()
                local current_value = inst.replica.hutao_com_healing_grove:GetCurrent()
                if current_value <= 0 then
                    root:Hide()
                else
                    display_text:SetString(GetDisplayStr(current_value))
                    root:Show()
                end
            end
            txt_update_fn()
            root.inst:ListenForEvent("hutao_com_healing_grove_replica_update",txt_update_fn,inst)
        ----------------------------------------------------------------------------------------------------------------
        return root
    end
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--- hook health and sanity 的关键API
    local function hook_health_and_sanity(inst)

        ----------------------------------------------------------------------------
        -- health
            local old_health_DoDelta = inst.components.health.DoDelta
            inst.components.health.DoDelta = function(self, value,...)
                if value < 0 and not self.skip_hutao_healing_grove then
                    local current_shell = self.inst.components.hutao_com_healing_grove:GetCurrent()
                    if current_shell > 0 then
                        value = self.inst.components.hutao_com_healing_grove:GetHealthDeltaFixed(value) or value
                        self.inst.components.hutao_com_healing_grove:HealthDownBlocked()
                    end
                end
                return old_health_DoDelta(self, value,...)
            end
            function inst.components.hutao_com_healing_grove:HealthDoDelta(value,...)
                self.inst.components.health.skip_hutao_healing_grove = true
                self.inst.components.health:DoDelta(value,...)
                self.inst.components.health.skip_hutao_healing_grove = false
            end
        ----------------------------------------------------------------------------
        -- sanity
            local old_sanity_DoDelta = inst.components.sanity.DoDelta
            inst.components.sanity.DoDelta = function(self, value,...)
                if value < 0 and not self.skip_hutao_healing_grove then
                    local current_shell = self.inst.components.hutao_com_healing_grove:GetCurrent()
                    if current_shell > 0 then                        
                        value = self.inst.components.hutao_com_healing_grove:GetSanityDeltaFixed(value) or value
                        self.inst.components.hutao_com_healing_grove:SanityDownBlocked()
                    end
                end
                return old_sanity_DoDelta(self, value,...)
            end
            function inst.components.hutao_com_healing_grove:SanityDoDelta(value,...)
                self.inst.components.sanity.skip_hutao_healing_grove = true
                self.inst.components.sanity:DoDelta(value,...)
                self.inst.components.sanity.skip_hutao_healing_grove = false
            end
        ----------------------------------------------------------------------------




    end
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--- 
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

return function(inst)
    inst:DoTaskInTime(1,function()
        if inst == ThePlayer and inst.HUD then
            CreateHUD(inst)
        end
    end)
    if not TheWorld.ismastersim then
        return
    end
    inst:AddComponent("hutao_com_healing_grove")
    inst:ListenForEvent("hutao_master_postinit", hook_health_and_sanity)
end